import request from '@/utils/request'
import type { PageResult } from '@/types'

export interface SysUser {
  id?: number
  username: string
  password?: string
  realName: string
  roleId: number
  roleName?: string
  roleCode?: string
  phone?: string
  avatar?: string
  status: number
  createTime?: string
  updateTime?: string
}

/**
 * 分页查询用户
 */
export function getUserPage(params: any) {
  return request<PageResult<SysUser>>({
    url: '/user/page',
    method: 'get',
    params
  })
}

/**
 * 获取用户详情
 */
export function getUserById(id: number) {
  return request<SysUser>({
    url: `/user/${id}`,
    method: 'get'
  })
}

/**
 * 新增用户
 */
export function addUser(data: SysUser) {
  return request<void>({
    url: '/user',
    method: 'post',
    data
  })
}

/**
 * 更新用户
 */
export function updateUser(data: SysUser) {
  return request<void>({
    url: '/user',
    method: 'put',
    data
  })
}

/**
 * 删除用户
 */
export function deleteUser(id: number) {
  return request<void>({
    url: `/user/${id}`,
    method: 'delete'
  })
}

/**
 * 重置密码
 */
export function resetPassword(id: number) {
  return request<void>({
    url: `/user/reset-password/${id}`,
    method: 'post'
  })
}

/**
 * 获取当前用户信息
 */
export function getUserProfile() {
  return request<SysUser>({
    url: '/user/profile',
    method: 'get'
  })
}

/**
 * 更新当前用户信息
 */
export function updateUserProfile(data: SysUser) {
  return request<void>({
    url: '/user/profile',
    method: 'put',
    data
  })
}

/**
 * 修改密码（首次登录或主动修改）
 */
export function changePassword(data: { oldPassword?: string; newPassword: string }) {
  return request<void>({
    url: '/user/change-password',
    method: 'post',
    data
  })
}

